﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMSSyncData.Model;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.SqlDB
{
    public class Tddev119DAL : BaseSqlDBDAL<tddev119>
    {
        public List<tddev119> GetSyncData()
        {
            var sql = @"SELECT os.RelatedBill AS orno,os.SetNo AS  oset, os.RowNo AS pono,os.SN AS seqn ,sr.ProductNumber AS item,' ' clot,sr.SelectQty AS qoro,os.Storage_SN  AS cwar,isnull(os.Ext3,' ') AS revi,
  '1'AS host,' ' AS mess ,GETDATE() AS edte1,GETDATE() AS wdte1,GETDATE() AS wdte2,0 AS refcntu,0 AS refcntd
  FROM [dbo].[wms_T_OutStock] os
  inner JOIN [dbo].[wms_T_OutStockRows] sr ON os.BillNo=sr.BillNo  and os.SN=sr.SN  and os.RowNo=sr.RowNo
  WHERE os.Status='1' AND os.BillType='销售计划'";
            var result = SqlDBHelper.GetDataSet(sql).Tables[0];
            var list = MakeTablePackage(result);
            return list;
        }

        public bool UpdateUploadStatus(List<tddev119> list)
        {
            var result = false;
            //var billNoList = list.Select(x => x.orno).Distinct();
            if (list.Count() > 0)
            {
                var sqlWhere = "where ";
                foreach (var item in list)
                {
                    sqlWhere = sqlWhere + " ( RelatedBill='" + item.orno + "' and  sn=" + item.seqn + " and rowno=" + item.pono + " and SetNo='"+item.oset+"') or";
                }
                sqlWhere = sqlWhere.TrimEnd('r');
                sqlWhere = sqlWhere.TrimEnd('o');

                var sql = "UPDATE  wms_T_OutStock SET Status='2' " + sqlWhere;
                var updateRes = SqlDBHelper.ExcuteSQL(sql);
                if (updateRes > 0)
                {
                    result = true;
                }
            }
            return result;

        }
    }
}
